#include "stdafx.h"

#include <string>
#include <vector>
#include <iostream>
#include <limits>

#include "Menu.h"
#include "baseCard.h"
#include "baseToken.h"

using namespace std;

Menu::Menu(string const &new_title, unsigned int nOptions, const char * const * pOptions)
{
    this->Title = new_title;
    for ( unsigned int i = 0; i < nOptions; i++ )
    {
        Options.push_back(pOptions[i]);
    }
}

Menu::Menu(string new_title, vector<baseCard> new_Options)
{
    this->Title = new_title;
    for (vector<baseCard>::iterator it = new_Options.begin(); it!=new_Options.end(); it++)
    {
        baseEffect* effect = it->getEffect();

        if(effect != NULL)
            Options.push_back(it->getName() + " - " + it->getEffect()->getDescription());
        else
            Options.push_back(it->getName());
    }
}

Menu::Menu(string new_title, vector<basePlayer*> new_Options)
{
    this->Title = new_title;
    for (vector<basePlayer*>::iterator it = new_Options.begin(); it!=new_Options.end(); it++)
    {
        Options.push_back((*it)->getName());
    }
}

void Menu::displayMenu()
{
    printf("\n-->%s\n", this->Title.c_str());

    /// display all items
    for ( unsigned int i = 0; i < this->Options.size(); i++ )
    {
        printf(" %d - %s\n", i+1, this->Options[i].c_str());
    }

    printf(" 0 - Exit Menu\nChoice: ");
}

unsigned int Menu::getUserSelection()
{
    unsigned int selection;

    this->displayMenu();

    /// get user selection
    cout.flush();
    while ( ! ( cin >> selection ) || selection < 0 || selection > this->Options.size() )
    { 
        if ( cin.eof() )
            return 0; //< ^D  (^Z sous windows); Fin du flux d'entree!
        else 
            if ( cin.fail() )
            { 
                printf("Invalid Choice, Retry: ");
                cin.clear(); 
                cin.ignore( numeric_limits<streamsize>::max(), '\n' );
            }
            else
                printf("Invalid Choice, Retry: ");
    }

    return selection;
}
